#include "playscene.h"
#include <QtDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>


PlayScene::PlayScene(int levelNum)
{
    QString str = QString("进入了第 %1 关").arg(levelNum);
    qDebug() << str;
    this->levelNum = levelNum;

    // 配置
    this->setFixedSize(320, 588);
    this->setWindowTitle("翻金币场景");
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    // 菜单
    QMenuBar * bar = new QMenuBar();
    setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");
    QAction  * exitAction = startMenu->addAction("退出");

    // 点击退出，实现退出
    connect(exitAction, &QAction::triggered, [=](){
        this->close();
    });

    // 返回音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);
    // 返金币音效
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav", this);
    // 胜利音效
    QSound *winSound = new QSound(":/res/LevelWinSound.wav", this);

    // 返回按钮
    MyPushButton * backButoon = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backButoon->setParent(this);
    backButoon->move(this->width()-backButoon->width(), this->height()-backButoon->height());

    // 实现返回按钮功能
    connect(backButoon, &QPushButton::clicked, [=](){
        // 播放返回音效
        backSound->play();

        // 延时返回
        QTimer::singleShot(500, this, [=](){
            emit this->chooseSceneBack();
        });
    });

    // 显示当前关卡数
    QLabel * lab = new QLabel;
    lab->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    lab->setFont(font);
    QString str1 = QString("Level: %1").arg(this->levelNum);
    lab->setText(str1);
    lab->setGeometry(30, this->height()-50, 120, 50);

    dataConfig config;
    // 初始化每个关卡的二维数组
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            this->gameArray[i][j] = config.mData[this->levelNum][i][j];
        }
    }

    // 胜利图片显示
    QLabel * winLabel = new QLabel;
    QPixmap temPix;
    temPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,temPix.width(), temPix.height());
    winLabel->setPixmap(temPix);
    winLabel->setParent(this);
    winLabel->move((this->width() - temPix.width())*0.5, -temPix.height());

    // 显示金币背景图案
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            // 绘制背景图片
            QPixmap pix = QPixmap(":/res/BoardNode.png");
            QLabel * label = new QLabel;
            label->setGeometry(0, 0, pix.width(), pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57+i*50, 200+j*50);

            // 创建金币
            QString str;
            if(this->gameArray[i][j] == 1)
            {
                str = ":/res/Coin0001.png";
            }
            else
            {
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(57+i*50, 200+j*50);

            // 给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j]; // 正面1，反面0

            // 将金币放入到金币的二维数组中，以便后期的维护
            coinBtn[i][j] = coin;

            // 点击金币，进行翻转
            connect(coin, &MyCoin::clicked, [=](){
                // 翻金币音效
                flipSound->play();

                coin->changeFlag();
                this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;

                // 翻转周围金币,延时
                QTimer::singleShot(300, this, [=](){
                    // 周围右侧的金币翻转的条件
                    if(coin->posX +1 <=3)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY] = this->gameArray[coin->posX+1][coin->posY] == 0 ? 1 : 0;
                    }
                    // 周围左侧的金币翻转的条件
                    if(coin->posX -1 >= 0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY] = this->gameArray[coin->posX-1][coin->posY] == 0 ? 1 : 0;
                    }
                    // 周围上侧的金币翻转的条件
                    if(coin->posY +1 <=3)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1] = this->gameArray[coin->posX][coin->posY+1] == 0 ? 1 : 0;
                    }
                    // 周围下侧的金币翻转的条件
                    if(coin->posY -1 >= 0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1] = this->gameArray[coin->posX][coin->posY-1] == 0 ? 1 : 0;
                    }

                    // 判断游戏是否胜利
                    this->isWin = true;
                    for(int i=0; i<4; i++)
                    {
                        for(int j=0; j<4; j++)
                        {
                            if(coinBtn[i][j]->flag == false)
                            {
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if(this->isWin == true)
                    {
                        for(int i=0; i<4; i++)
                        {
                            for(int j=0; j<4; j++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }
                        }
                        // 胜利音效
                        winSound->play();

                        // 将胜利图片移动下来
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel, "geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114, winLabel->width(), winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });
            });
        }
    }

}

void PlayScene::paintEvent(QPaintEvent *)
{
    // 背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    // 标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);  // 缩放
    painter.drawPixmap(10,30,pix);
}











